<template>
    <div class="app-container calendar-list-container" v-loading="loading" element-loading-text="正在导出数据">
    <!-- filter 条件查询 -->
    <div class="filter-container">

      <el-input
        @keyup.enter.native="getData"
        clearable
        v-model="param.workOrderNo"
        style="width: 150px;margin-right:15px;margin-top:7px"
        class="filter-item"
        placeholder="请输入工单单号"
      ></el-input>

      <el-input
        @keyup.enter.native="getData"
        clearable
        v-model="param.orderType"
        style="width: 150px;margin-right:15px;margin-top:7px"
        class="filter-item"
        placeholder="请输入工单单别"
      ></el-input>

      <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="getData">查询</el-button>

    </div>
    
    <!-- table 表格 -->
    <el-table  :data="tableInfo" v-loading="loadingList" element-loading-text="给我一首歌的时间" stripe border fit highlight-current-row style="width: 100%">
      <el-table-column  label="序号" type="index" align="center" width="80px"/>
      <el-table-column label="设备号"  prop="machineId" align="center"/>
      <el-table-column label="工序"  prop="processName" align="center"/>
      <el-table-column  label="班次" prop='planNo' align="center"/>
      <el-table-column  label="工单号" prop='orderNo' align="center"/>
      <el-table-column  label="标准工时" prop='' align="center"/>
      <el-table-column  label="实际工时" prop='' align="center"/>
      <el-table-column  label="工时达成率" prop='' align="center"/>
    </el-table>
   
   <!-- pagination 分页 -->
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="listQuery.page"
        :page-sizes="[5,10, 20, 30, 40, 100, 300, 500]"
        :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

     <el-divider></el-divider>

    <el-button type="success" size="small" @click="hExportExcel">导出数据Excel</el-button>

  </div>
</template>

<script>
import { findSchedule } from '@/api/CNCmachine.js'
export default {
    name: 'manArate',
    data() {
        return {
            total: 0,                           //数据的总数
            listQuery: {
                page: 1,                          //页码
                limit: 10                        //每页显示的数据条数
            },
            param: {
              workOrderNo:'',                   //工单单号
              orderType:''                      //工单单别
            },
            tableInfo: [],
            loadingList: false,
            loading: false
        }
    },
    methods: {
    transformDat(res) {
      // console.log('%c导出质量系数excel数据:','color:green;font-size:16px;')
      // console.log(res)
      const mapInfo = {
        index: '序号',
        machineId: '设备号',
        processName: '工序',
        planNo: '班次',
        orderNo: '工单号',
        ultimately: '标准工时',
        ultimately1: '实际工时',
        ultimately2: '工时达成率'
      }
      // 对data进行格式化
      // 取出所有的属性名
      const props = Object.keys(res[0]) // ['id','']
      console.log(props)
      // 将属性名从英文转为中文
      const header = props.map(zhkey => mapInfo[zhkey])
      console.log('中文的表头', header)
      // const data=[['xxxx','总裁办'，'13800000']]
      const data = res.map(obj => Object.values(obj))
      console.log(data)
      return { header, data }
    },
    // 导出excel
    async hExportExcel() {
      this.loading = true
      let query = Object.assign({},this.listQuery)
      query.date = query.date == null ? '' : query.date
      query.page = 1
      query.limit = 10000
      if(this.param.workOrderNo){query.workOrderNo = this.param.workOrderNo}
      if(this.param.orderType){query.orderType = this.param.orderType}
      console.log('%c导出excel传的参数query:','color:green;font-size:16px;')
      console.log(query)

      let arr = await findSchedule(query)
      const { header, data } = this.transformDat(this.setSort(arr.data.list))
        import('@/vendor/Export2Excel').then(excel => {
        // excel表示导入的模块对象
        console.log(excel)
        excel.export_json_to_excel({
        header, // 表头 必填
        data, // 具体数据 必填
        filename: '工时达成率excel', // 文件名称
        autoWidth: true, // 宽度是否自适应
        bookType: 'xlsx' // 生成的文件类型
        })
      })
      this.loading = false
    },
    // 排序函数
    setSort(arr){
      let newArr = JSON.parse(JSON.stringify(arr))
      let arr1 = []
      newArr.forEach((item,index)=>{
        let obj = {
          index: index+1,
          machineId: item.machineId,
          processName: item.processName,
          planNo: item.planNo,
          orderNo: item.orderNo,
          ultimately: '',
          ultimately1: '',
          ultimately2: '',
        }
        arr1.push(obj)
      })
      return arr1
    },
    // 搜索按钮绑定的函数，向服务器发送表单数据
    // 所有传输的表单字段都不是必须的，按需查询
    async handleFilter() {
      this.loadingList = true
      let query = Object.assign({},this.listQuery)

      if(this.param.workOrderNo){query.workOrderNo = this.param.workOrderNo}
      if(this.param.orderType){query.orderType = this.param.orderType}

      console.log('%c/back/machineBack/findSchedule接口传参:','color:red;font-size:16px;')
      console.log('query',query)

      let data = await findSchedule(query)
      let errMsg = data.msg
      if(data.code == '1'){
        let datas = JSON.parse(JSON.stringify(data.data.list))
        datas.forEach(item => {
          item.orderNo = item.orderType + '-' + item.workOrderNo
        });
        this.tableInfo = datas
        this.total = data.data.totle
      } else{
        this.$message({
          type: 'warning',
          message: errMsg
        })
      }
      this.loadingList = false
    },
    // 节流，fail
    // getTableInfo() {
    //   throttle(this.handleFilter())
    // },
    // table表单下方的页码组件改变每页显示的数据条数
     handleSizeChange(val) {
      this.listQuery.limit = val
      this.handleFilter()
    },
    // table表单下方的页码组件改变页面
     handleCurrentChange(val) {
      this.listQuery.page = val
      this.handleFilter()
    },
    getData() {
      this.listQuery.page = 1
      this.listQuery.limit = 10
      this.handleFilter()
    },
  },
  // 进入页面获取table信息
  mounted() {
    this.handleFilter()
  }
}
</script>

<style>

</style>